package com.droidlogic.mboxlauncher;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

public class MyRelativeLayout extends RelativeLayout implements ViewTreeObserver.OnGlobalLayoutListener {
    private boolean isSwitchAnimRunning = false;
    private boolean layoutCompleted = false;
    private Context mContext = null;
    private float mElevation;
    private Intent mIntent = null;
    private boolean mIsAddButton = false;
    private int mNumber = -1;
    private float mScale;
    private float mShadowScale;
    private int mType = -1;

    public MyRelativeLayout(Context context) {
        super(context);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        setFocusable(true);
        setFocusableInTouchMode(true);
        setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
    @Override
    public void onGlobalLayout() {
        getViewTreeObserver().removeOnGlobalLayoutListener(MyRelativeLayout.this);
        this.layoutCompleted = true;
        if (isFocused()) {
            setHoverView();
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_DOWN ) {
//            Log.d("MediaBoxLauncher","click keyCode="+event.getKeyCode());
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    if (this.mNumber != -1 && this.mNumber % 6 == 0) {
                        this.isSwitchAnimRunning = true;
                        ((Launcher) this.mContext).switchSecondScren(0);
                    }
                    break;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    if (this.mNumber != -1 && (this.mNumber % 6 == 5 || this.mNumber == ((ViewGroup) getParent()).getChildCount() - 1)) {
                        this.isSwitchAnimRunning = true;
                        ((Launcher) this.mContext).switchSecondScren(1);
                    }
                    break;
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
//                    Log.d("MediaBoxLauncher","KEYCODE_ENTER>>"+mType);
                    if (this.mType != 7) {
                        ((Launcher) this.mContext).saveHomeFocus(this);
                    }
                    switch (this.mType) {
                        case 0:
                            Intent intent = new Intent();
                            intent.setComponent(new ComponentName("com.core.lntmobile", "com.core.lntmobile.activities.SplashActivity"));
                            try {
                                this.mContext.startActivity(intent);
                            }catch (Exception e){
                                Log.e("Launcher","没有找到app");
                            }

                            break;
                        case 1:
                            showSecondScreen(2);
                            break;
                        case 2:
                            Intent vintent = new Intent();
                            vintent.setComponent(new ComponentName("com.android.vending", "com.google.android.finsky.activities.MainActivity"));
                            try {
                                this.mContext.startActivity(vintent);
                            }catch (Exception e){
                                Log.e("Launcher","没有找到app");
                            }

                            break;
                        case 3:
                            Intent mintent = new Intent();
                            mintent.setComponent(new ComponentName("com.rockchips.mediacenter", "com.rockchips.mediacenter.activity.MainActivity"));

                            try {
                                this.mContext.startActivity(mintent);
                            }catch (Exception e){
                                Log.e("Launcher","没有找到app");
                            }
                            break;
                        case 4:
                            showSecondScreen(5);
                            break;
                        case 5:
                            showSecondScreen(4);
                            break;
                        case 6:
                        case 7:
                            if (this.mIntent == null) {
                                if (this.mIsAddButton) {
                                    ((Launcher) this.mContext).startCustomScreen(this);
                                }
                            } else {
                                ComponentName cameraCom = new ComponentName("com.android.camera2", "com.android.camera.CameraLauncher");
                                Log.i("scott", "mIntent.getComponent().flattenToString() =" + this.mIntent.getComponent().flattenToString());
                                Log.i("scott", "mIntent.getComponent().getPackageName().flattenToString() =" + this.mIntent.getComponent().getPackageName());
                                if (!this.mIntent.getComponent().flattenToString().equals(Launcher.COMPONENT_TV_APP)) {
                                    if (!this.mIntent.getComponent().getPackageName().equals(Launcher.PACKAGE_MFC)) {
                                        if (this.mIntent.getComponent().equals(cameraCom)) {
                                            if (this.mContext.getPackageManager().getComponentEnabledSetting(cameraCom) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
                                                try {
                                                    this.mContext.startActivity(this.mIntent);
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        } else {
                                            try {
                                                this.mContext.startActivity(this.mIntent);
                                                String[] temp = this.mIntent.getComponent().flattenToString().split("/");
                                                if (temp.length > 0 && temp[0].equals("com.android.gl2jni")) {
                                                    Launcher.isLaunchingThomasroom = true;
                                                }
                                            }catch (Exception e){
                                                Log.e("Launcher","没有找到app");
                                            }

                                        }
                                    } else {
                                        ((Launcher) this.mContext).startMFC();
                                    }
                                } else {
                                    ((Launcher) this.mContext).startTvApp();
                                }
                            }
                            break;
                        case 8:
                            Intent yintent = new Intent();
                            yintent.setComponent(new ComponentName("com.google.android.youtube.tv", "com.google.android.apps.youtube.tv.activity.ShellActivity"));
                            try {
                                this.mContext.startActivity(yintent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            break;
                        case 9:
                            Intent Bintent = new Intent();
                            Bintent.setComponent(new ComponentName("com.famousbluemedia.yokee", "com.famousbluemedia.yokee.splash.SplashActivity"));
                            try {
                                this.mContext.startActivity(Bintent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            break;
                        case 10:
                            Intent Kintent = new Intent();
                            Kintent.setComponent(new ComponentName("com.netflix.mediaclient", "com.netflix.mediaclient.ui.launch.UIWebViewActivity"));
                            try {
                                this.mContext.startActivity(Kintent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            break;
                        case 11:
                            Intent meintent = new Intent();
                            meintent.setClassName("com.android.tv.settings", "com.android.tv.settings.MainSettings");
                            meintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            try {
                                this.mContext.startActivity(meintent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            break;
                    }
            }
        }
        return super.dispatchKeyEvent(event);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == 0 || event.getAction() != 1) {
            return true;
        }
        switch (this.mType) {
            case 0:
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.core.lntmobile", "com.core.lntmobile.activities.SplashActivity"));
                try {
                    this.mContext.startActivity(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            case 1:
                showSecondScreen(2);
                break;
            case 2:
                Intent vintent = new Intent();
                vintent.setComponent(new ComponentName("com.android.vending", "com.google.android.finsky.activities.MainActivity"));
                try {
                    this.mContext.startActivity(vintent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            case 3:
                Intent mintent = new Intent();
                mintent.setComponent(new ComponentName("com.rockchips.mediacenter", "com.rockchips.mediacenter.activity.MainActivity"));
                try {
                    this.mContext.startActivity(mintent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            case 4:
                showSecondScreen(5);
                break;
            case 5:
                showSecondScreen(4);
                break;
            case 6:
            case 7:
                ComponentName cameraCom = new ComponentName("com.android.camera2", "com.android.camera.CameraLauncher");
                if (this.mIntent == null) {
                    if (this.mIsAddButton) {
                        ((Launcher) this.mContext).startCustomScreen(this);
                        break;
                    }
                } else {
                    Log.i("scott", "mIntent.getComponent().flattenToString() =" + this.mIntent.getComponent().flattenToString());
                    Log.i("scott", "mIntent.getComponent().getPackageName().flattenToString() =" + this.mIntent.getComponent().getPackageName());
                    if (!this.mIntent.getComponent().flattenToString().equals(Launcher.COMPONENT_TV_APP)) {
                        if (!this.mIntent.getComponent().getPackageName().equals(Launcher.PACKAGE_MFC)) {
                            if (this.mIntent.getComponent().equals(cameraCom)) {
                                if (this.mContext.getPackageManager().getComponentEnabledSetting(cameraCom) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
                                    try {
                                        this.mContext.startActivity(this.mIntent);
                                        break;
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                }
                            } else {
                                try {
                                    this.mContext.startActivity(this.mIntent);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }

                                break;
                            }
                        } else {
                            ((Launcher) this.mContext).startMFC();
                            break;
                        }
                    } else {
                        ((Launcher) this.mContext).startTvApp();
                        break;
                    }
                }
                break;
            case 8:
                Intent yintent = new Intent();
                yintent.setComponent(new ComponentName("com.google.android.youtube.tv", "com.google.android.apps.youtube.tv.activity.ShellActivity"));
                try {
                    this.mContext.startActivity(yintent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            case 9:
                Intent Bintent = new Intent();
                Bintent.setComponent(new ComponentName("com.famousbluemedia.yokee", "com.famousbluemedia.yokee.splash.SplashActivity"));
                try {
                    this.mContext.startActivity(Bintent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            case 10:
                Intent Kintent = new Intent();
                Kintent.setComponent(new ComponentName("com.netflix.mediaclient", "com.netflix.mediaclient.ui.launch.UIWebViewActivity"));
                try {
                    this.mContext.startActivity(Kintent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
            case 11:
                Intent meintent = new Intent();
                meintent.setClassName("com.android.tv.settings", "com.android.tv.settings.MainSettings");
                meintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    this.mContext.startActivity(meintent);
                }catch (Exception e){
                    e.printStackTrace();
                }

                break;
        }
        return false;
    }
    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        if (!gainFocus) {
            ScaleAnimation anim = new ScaleAnimation(1.07f, 1.0f, 1.07f, 1.0f, 1, 0.5f, 1, 0.5f);
            anim.setZAdjustment(1);
            anim.setDuration(70);
            anim.setStartTime(0);
            startAnimation(anim);
        } else if (this.layoutCompleted) {
            setHoverView();
        }
    }
    public void setType(int type) {
        this.mType = type;
        switch (this.mType) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 8:
            case 9:
            case 10:
            case 11:
                this.mScale = 1.07f;
                this.mElevation = 40.0f;
                this.mShadowScale = 1.0f;
                return;
            case 6:
                this.mScale = 1.1f;
                this.mElevation = 30.0f;
                this.mShadowScale = 0.9f;
                return;
            case 7:
                this.mScale = 1.1f;
                this.mElevation = 36.0f;
                this.mShadowScale = 0.9f;
                return;
            default:
                return;
        }
    }

    public void setIntent(Intent intent) {
        this.mIntent = intent;
    }

    private void showSecondScreen(int mode) {
        ((Launcher) this.mContext).setHomeViewVisible(false);
        ((Launcher) this.mContext).setShortcutScreen(mode);
    }

    private void setHoverView() {
        ((Launcher) this.mContext).getHoverView().setHover(this);
    }

    public void setNumber(int number) {
        this.mNumber = number;
    }

    public void setAddButton(boolean isAdd) {
        this.mIsAddButton = isAdd;
    }

    public int getType() {
        return this.mType;
    }

    public float getScale() {
        return this.mScale;
    }

    public float getElevation() {
        return this.mElevation;
    }

    public float getShadowScale() {
        return this.mShadowScale;
    }

}